home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / bit / src / clock_mail.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  9KB  |  446 lines

  1. /*
  2.  * $Id: clock_mail.c,v 0.91 1994/02/20 00:53:31 zhao Pre-Release $
  3.  *
  4.  *. This file is part of BIT shareware package. After the two weeks of
  5.  *  free evaluation period, you are encouraged (required) to register
  6.  *  your copy for a small registration fee, which is $35 for personal use
  7.  *  and $50 for commercial, government and institutional use.
  8.  *
  9.  *  Copyright(c) 1993, 1994 by T.C. Zhao.
  10.  *  All rights reserved.
  11.  *
  12.  *  Permission to use, copy, and distribute this software in its entirety
  13.  *  for non-commercial purposes is hereby granted, provided that the
  14.  *  above shareware and copyright notices and this permission notice
  15.  *  appear in all copies and their documentation.
  16.  *
  17.  *  This software may be modified for your own use, but modified versions
  18.  *  may not be distributed without prior consent of the author.
  19.  *
  20.  *  This software is provided "as is" without expressed or implied
  21.  *  warranty of any kind.
  22.  *
  23.  *.
  24.  *
  25.  * open a window and show a clock and a mailbox. Upon receiving ESC key, this
  26.  * process will die and optionally send iSIGUSR2 to its parant if whose pid
  27.  * is known. QKEY will simple quit CKEY coredump
  28.  */
  29. #ifndef lint
  30. char *id = "$Id: clock_mail.c,v 0.91 1994/02/20 00:53:31 zhao Pre-Release $";
  31. #endif
  32.  
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <unistd.h>
  37. #include <sys/types.h>
  38. #include "gl/gl.h"
  39. #include "gl/device.h"
  40. #include <sys/stat.h>
  41. #include <signal.h>
  42. #include <errno.h>
  43. #include "forms.h"
  44.  
  45. #include "bitmaps/newmail.xbm"
  46. #include "bitmaps/nomail.xbm"
  47.  
  48. void gui_init(void);
  49.  
  50. #ifndef DEF_SLEEP
  51. #define DEF_SLEEP    60
  52. #endif
  53.  
  54. #ifndef LOCK_FILE
  55. #define LOCK_FILE  ".iclock.lc"
  56. #endif
  57.  
  58. int interval = DEF_SLEEP;
  59. struct stat buf;
  60. static off_t size = 0;
  61. static int fcol = 47;        /* forground color */
  62. static char *mailbox = 0;
  63. static FL_FORM *clock_mail;
  64. static FL_OBJECT *bitmap;
  65. static long myqread(int, short *, long);
  66. static void create_form_clock_mail(void);
  67. static char *gcmd;
  68.  
  69. #define COLOR_NOMAIL 991
  70. #define COLOR_MAIL   992
  71. #define COLOR_BK     993
  72.  
  73. /* set default colors */
  74. void
  75. map_colors(void)
  76. {
  77.     fl_init();
  78.     fl_mapcolor(COLOR_NOMAIL, 0, 0, 0);
  79.     fl_mapcolor(COLOR_MAIL, 0, 0, 255);
  80.     fl_mapcolor(COLOR_BK, 163, 163, 163);
  81.     fl_mapcolor(fcol, 163, 163, 163);
  82.     fl_mapcolor(0, 0, 0, 0);
  83. }
  84.  
  85. /* show no mail bitmap */
  86. void
  87. nomail_bitmap(void)
  88. {
  89.     fl_set_bitmap(bitmap, BM_X, BM_Y, nomail_bits);
  90.     fl_set_object_color(bitmap, COLOR_NOMAIL, COLOR_BK);
  91.     fl_redraw_object(bitmap);
  92. }
  93.  
  94. /* show no mail bitmap */
  95. void
  96. newmail_bitmap(void)
  97. {
  98.     fl_set_bitmap(bitmap, BM_Y, BM_Y, newmail_bits);
  99.     fl_set_object_color(bitmap, COLOR_MAIL, COLOR_BK);
  100.     ringbell();
  101.     fl_redraw_object(bitmap);
  102. }
  103.  
  104. /* called to reset mailbox */
  105. /* ARGSUSED */
  106. void
  107. reset_mail(FL_OBJECT * a, long ha)
  108. {
  109.     size = stat(mailbox, &buf) ? 0 : buf.st_size;
  110.     nomail_bitmap();
  111. }
  112.  
  113. /* see if new mail */
  114. void
  115. check_mail(void)
  116. {
  117.     static int newmail = 0;
  118.  
  119.     if (stat(mailbox, &buf))
  120.       {
  121.       newmail = 0;
  122.       reset_mail(0, 0);
  123.       return;
  124.       }
  125.     if (newmail)
  126.       {
  127.       if (buf.st_size < size)
  128.         {            /* other process read */
  129.         newmail = 0;
  130.         reset_mail(0, 0);
  131.         }
  132.       }
  133.     else
  134.       {
  135.       if (buf.st_size > size)
  136.         {            /* new mail arrived */
  137.         newmail = 1;
  138.         size = buf.st_size;
  139.         newmail_bitmap();
  140.         }
  141.       }
  142. }
  143.  
  144.  
  145. static int bitlaunched;
  146. char lockfile[1024];
  147.  
  148. /* ARGSUSED */
  149. void
  150. finish_up(int sig)
  151. {
  152.     if (bitlaunched && access(lockfile, F_OK) == 0)
  153.     remove(lockfile);
  154.     gexit();
  155.     exit(0);
  156. }
  157.  
  158. /* check if there is lock file in existence. Need to do this
  159.  * because if it is bit launched, only can allow one copy to run
  160.  */
  161. void
  162. check_status(void)
  163. {
  164.     char *home, line[100];
  165.     FILE *fp;
  166.     int cid;
  167.  
  168.     if (LOCK_FILE[0] != '/')
  169.       {
  170.       home = getenv("HOME");
  171.       if (!home)
  172.         {
  173.         fputs("Unable to find home\n", stderr);
  174.         exit(1);
  175.         }
  176.       strcpy(lockfile, home);
  177.       strcat(lockfile, "/");
  178.       strcat(lockfile, LOCK_FILE);
  179.       }
  180.     else
  181.     strcpy(lockfile, LOCK_FILE);
  182.  
  183.     if (bitlaunched && access(lockfile, F_OK) == 0)
  184.       {
  185.       fp = fopen(lockfile, "r");
  186.       cid = 0;
  187.       if (fgets(line, 100, fp))
  188.           cid = atoi(line);
  189.       fclose(fp);
  190.       if (cid && kill(cid, 0) == 0)
  191.         {
  192.         fprintf(stderr, "%s is already runing with pid=%s", gcmd, line);
  193.         exit(0);
  194.         }
  195.       else if (!cid || errno == ESRCH)
  196.         {
  197.         remove(lockfile);
  198.         }
  199.       }
  200.  
  201.     (void) signal(SIGUSR1, finish_up);
  202.     (void) signal(SIGTERM, finish_up);
  203. }
  204.  
  205. /*********************************************************************/
  206. char *optstr = "fbs:m:p:";    /* b undocumented */
  207. char *optinfo = "%s [-f] [-s sec] [-m mbox] [x y [w h]]\n";
  208. void
  209. usage(char *c)
  210. {
  211.     fprintf(stderr, optinfo, c);
  212.     exit(1);
  213. }
  214. static parent = -1;
  215. int
  216. main(int argc, char *argv[])
  217. {
  218.     short val;
  219.     extern char *optarg;
  220.     extern optind, opterr;
  221.     char *qq;
  222.     int frame = 0, done = 0, c;
  223.     int x1 = -1, y1 = -1, w = 190, h = 50;
  224.     FILE *fp;
  225.  
  226.  
  227.     gcmd = argv[0];
  228.  
  229.     opterr = 0;
  230.     while ((c = getopt(argc, argv, optstr)) != EOF)
  231.       {
  232.       switch (c)
  233.         {
  234.         case 'c':
  235.         fcol = atoi(optarg);
  236.         break;
  237.         case 's':
  238.         interval = atoi(optarg);
  239.         break;
  240.         case 'm':
  241.         mailbox = optarg;
  242.         break;
  243.         case 'p':
  244.         parent = atoi(optarg);
  245.         break;
  246.         case 'f':
  247.         frame = !frame;
  248.         break;
  249.         case 'b':
  250.         bitlaunched = 1;
  251.         break;
  252.         default:
  253.         usage(argv[0]);
  254.         break;
  255.         }
  256.       }
  257.     /*
  258.      * check_status simple checks to see if this program is already running,
  259.      * and quit if it is running AND is launched by bit
  260.      */
  261.     check_status();
  262.  
  263.  
  264.     if (optind < argc)
  265.       {
  266.       x1 = atoi(argv[optind]);
  267.       optind++;
  268.       }
  269.     if (optind < argc)
  270.       {
  271.       y1 = atoi(argv[optind]);
  272.       optind++;
  273.       }
  274.     if (optind < argc)
  275.       {
  276.       w = atoi(argv[optind]);
  277.       optind++;
  278.       }
  279.  
  280.     if (optind < argc)
  281.       {
  282.       h = atoi(argv[optind]);
  283.       optind++;
  284.       }
  285.     if (!mailbox)
  286.     mailbox = (qq = getenv("MAIL")) ? qq : getenv("MAILBOX");
  287.  
  288.     /* check */
  289.     if (!mailbox)
  290.       {
  291.       /* make up one on the fly */
  292.       static char mbox[1024], *user;
  293.       strcpy(mbox, "/usr/mail/");
  294.       user = (qq = getlogin())? qq : getenv("USER");
  295.       if (!user)
  296.         {
  297.         fputs("Cant't fine mailbox\n", stderr);
  298.         finish_up(1);
  299.         }
  300.       mailbox = strcpy(mbox, user);
  301.       }
  302.  
  303.     if (interval < 0)
  304.     interval = DEF_SLEEP;
  305.  
  306.     create_form_clock_mail();
  307.     map_colors();
  308.     reset_mail(0, 0);
  309.  
  310.     if (w > 0 && h > 0)
  311.       {
  312.       prefposition(x1, x1 + w - 1, y1, y1 + h - 1);
  313.       fl_show_form(clock_mail, FL_PLACE_FREE, frame, "clock & Mail");
  314.       }
  315.     else if (x1 > -100)
  316.       {
  317.       fl_set_form_position(clock_mail, x1, y1);
  318.       fl_show_form(clock_mail, FL_PLACE_POSITION, frame, "clock & Mail");
  319.       }
  320.     winset(clock_mail->window);
  321.     winpop();
  322.     fl_qdevice(ESCKEY);
  323.     fl_qdevice(QKEY);
  324.     fl_qdevice(RKEY);
  325.     fl_qdevice(CKEY);
  326.  
  327.     fl_qreset();
  328.  
  329.     if (bitlaunched)
  330.       {
  331.       if (!(fp = fopen(lockfile, "w")))
  332.         {
  333.         fprintf(stderr, "Can't create lock file\n");
  334.         finish_up(0);
  335.         }
  336.       fprintf(fp, "%d\n", getpid());
  337.       fclose(fp);
  338.       }
  339.  
  340.     while (!done)
  341.       {
  342.       long dev;
  343.       check_mail();
  344.       done = 1;
  345.       if ((dev = myqread(interval, &val, 123456)) == ESCKEY && val)
  346.         {
  347.         if (parent > 0)
  348.             kill(parent, SIGUSR1);
  349.         }
  350.       else if (dev == RKEY && val)
  351.         {
  352.         if (parent > 0)
  353.             kill(parent, SIGUSR2);
  354.         }
  355.       else if (dev == CKEY && val)
  356.         {
  357.         if (parent > 0)
  358.             kill(parent, SIGABRT);
  359.         }
  360.       else if (dev == QKEY && val)
  361.         {
  362.         ;        /* what to do ? */
  363.         }
  364.       else
  365.         {
  366.         done = 0;
  367.         }
  368.       }
  369.     finish_up(0);
  370.     return 0;
  371. }
  372.  
  373. /*
  374.  * myqreqd.c
  375.  *
  376.  * Read event queue within a time limit specified by seconds and mseconds.
  377.  * Default returned value is def. Nagative time disables time-out.
  378.  */
  379. #include <setjmp.h>
  380. #include <signal.h>
  381. static jmp_buf nokey;
  382. static int catch;
  383. static long returned;
  384. /* sysV */
  385. #define VOID void
  386. #define VAL
  387.  
  388. static long
  389. myqread(int seconds, short *val, long def)
  390. {
  391.     VOID trapalarm(int);
  392.     VOID(*Oint) (int);
  393.  
  394.     if (seconds <= 0)
  395.     return fl_qread(val);
  396.     *val = 1;
  397.     returned = def;
  398.     Oint = signal(SIGINT, trapalarm);
  399.     (void) signal(SIGALRM, trapalarm);
  400.     alarm((unsigned) seconds);
  401.     catch = 1;
  402.     if (setjmp(nokey) == 0)
  403.       {
  404.       fl_check_forms();
  405.       while (!fl_qtest())
  406.           fl_do_forms();
  407.       returned = fl_qread(val);
  408.       alarm(0);
  409.       }
  410.     else
  411.       {
  412.       catch = 0;
  413.       }
  414.     catch = 0;
  415.     (void) signal(SIGINT, Oint);
  416.     alarm(0);
  417.     return (returned);
  418. }
  419.  
  420. /* ARGSUSED */
  421. VOID
  422. trapalarm(int sig)
  423. {
  424.     alarm(0);
  425.     if (catch)
  426.     longjmp(nokey, 1);
  427.     return VAL;
  428. }
  429.  
  430.  
  431. static void
  432. create_form_clock_mail(void)
  433. {
  434.     FL_OBJECT *obj;
  435.     clock_mail = fl_bgn_form(FL_NO_BOX, 190.0, 85.0);
  436.     obj = fl_add_box(FL_UP_BOX, 0.0, 0.0, 190.0, 85.0, "");
  437.  
  438.     obj = fl_add_clock(FL_SQUARE_CLOCK, 25.0, 10.0, 70.0, 65.0, "");
  439.     fl_set_object_boxtype(obj, FL_ROUNDED_BOX);
  440.  
  441.     bitmap = fl_add_bitmap(FL_NORMAL_BITMAP, 105, 10, 70, 65, "");
  442.     obj = fl_add_button(FL_HIDDEN_BUTTON, 105.0, 10, 70.0, 65.0, "");
  443.     fl_set_call_back(obj, reset_mail, 0);
  444.     fl_end_form();
  445. }
  446.